﻿
namespace PlaylistCopy.Ioc
{
    using Autofac;
    using PlaylistCopy.Components;
    using PlaylistCopy.Localization;
    using PlaylistCopy.Logging;
    using PlaylistCopy.Services;
    using PlaylistCopy.Services.Impl;
    using PlaylistCopy.ViewModels;
    using PlaylistCopy.ViewModels.Impl;

    public class Container
    {
        private readonly IContainer container;

        public Container()
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterType<PlaylistImporter>().As<IPlaylistImporter>().SingleInstance().PropertiesAutowired();

            // modules
            containerBuilder.RegisterModule(new LoggingModule());

            // vms
            containerBuilder.RegisterType<MainWindowVm>().As<IMainWindowVm>().PropertiesAutowired(); 
            containerBuilder.RegisterType<SummaryWindowVm>().As<ISummaryWindowVm>().PropertiesAutowired();
            containerBuilder.RegisterType<AboutWindowVm>().As<IAboutWindowVm>().PropertiesAutowired();

            // services
            containerBuilder.RegisterType<DialogService>().As<IDialogService>().PropertiesAutowired(); ;
            containerBuilder.RegisterType<PlaylistService>().As<IPlaylistService>().PropertiesAutowired(); ;
            containerBuilder.RegisterType<NotificationService>().As<INotificationService>().PropertiesAutowired(); ;
            containerBuilder.RegisterType<DirectoryService>().As<IDirectoryService>().PropertiesAutowired(); ;
            containerBuilder.RegisterType<WindowFactory>().As<IWindowFactory>().SingleInstance().PropertiesAutowired(); ;
            containerBuilder.RegisterType<FileService>().As<IFileService>().PropertiesAutowired();
            containerBuilder.RegisterType<DateTimeProvider>().As<IDateTimeProvider>().PropertiesAutowired();

            containerBuilder.Register(ctx => (Localizer) LocalizerDelegate.Localize);

            this.container = containerBuilder.Build();
        }

        public T Resolve<T>()
        {
            return this.container.Resolve<T>();
        }
    }
}
